#include "MyDynamicArray.h"
#include <MyString.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

struct student
{
    MyStr name;
    int id;
    int age;
}; 
typedef struct student Stu;

Stu *CreateStu(const int id, const char *name, const int age)
{
    Stu *s = (Stu *)malloc(sizeof(Stu));
    if(s == NULL)
    {
        return NULL;
    }
    s->id = id;
    s->name = GetString(name);
    s->age = age;
    return s;
}

void ClearStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    ClearString(&s->name);
    free(s);
}

void PrintStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    printf("学生学号:%d 姓名%s 年龄:%d\n",s->id,s->name.string,s->age);
}

bool SortById(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    if(s1->id > s2->id)
        return true;
    return false;
}

bool SortByName(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    if(strcmp(s1->name.string,s2->name.string) > 0)
        return true;
    return false;
}

bool SortByAge(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    if(s1->age > s2->age)
        return true;
    return false;
}

bool CheckAge(void *ptr1,void *ptr2)
{
    long age = (long)ptr1;
    Stu *s = (Stu *)ptr2;
    if(s->age >= age)
    {
        return true;
    }
    return false;
}

struct teacher 
{
    int age;
    MyStr name;
    DArray StuArray;
};
typedef struct teacher Teach;

Teach *CreateTeach(const char *name,const int age)
{
    Teach *t = (Teach *)malloc(sizeof(Teach));
    if(t == NULL)
    {
        return NULL;
    }
    t->age = age;
    t->name = GetString(name);
    MyInitDynamicArray(&t->StuArray);
    return t;
}

void ClearTeach(void *ptr)
{
    Teach *t = (Teach *)ptr;
    ClearString(&t->name);
    t->StuArray.MyClearDynamicArray(&t->StuArray,ClearStu);
    free(t);
}

void AddStudent(Teach *t,Stu *s)
{
    t->StuArray.MyInsertArrayTail(&t->StuArray,s);
}

void PrintTeach(void *ptr)
{
    Teach *t = (Teach *)ptr;
    printf("老师姓名:%s 年龄:%d\n",t->name.string,t->age);
    printf("老师的学生信息:\n");
    t->StuArray.MyDisplayDynamicArray(&t->StuArray,PrintStu);
}

bool CheckNameContain(void *ptr1,void *ptr2)
{
    char *name = (char *)ptr1;
    Teach *t = (Teach *)ptr2;
    if(IsContains(&t->name,name) == true)
    {
        return true;
    }
    return false;
}


int main()
{
    // DArray a;
    // MyInitDynamicArray(&a);
    // for(int i = 0; i < 10; i++)
    // {
    //     MyInsertArrayTail(&a,(void *)(i+1));
    // }
    // MyInsertArrayTail(&a,CreateStu(1,"baoyiqi",22));
    // MyInsertArrayTail(&a,CreateStu(2,"lisi",23));
    // MyInsertArrayTail(&a,CreateStu(3,"zhangsan",20));
    // MyInsertArrayTail(&a,CreateStu(4,"wangwu",21));

    // MySort(&a,SortByAge);
    // MyRemoveByElement(&a,(void*)21,CheckAge,ClearStu);

    // MyDisplayDynamicArray(&a,PrintStu);
    // MyClearDynamicArray(&a,ClearStu);


    // Teach *t1 = CreateTeach("张老师",40);
    // AddStudent(t1,CreateStu(1,"baoyiqi",22));
    // AddStudent(t1,CreateStu(2,"zhangyu",21));
    // AddStudent(t1,CreateStu(3,"chengshi",24));
    // PrintTeach(t1);

    // Teach *t2 = CreateTeach("李老师",50);
    // AddStudent(t2,CreateStu(1,"xiaoming",22));
    // AddStudent(t2,CreateStu(2,"xiaohong",22));
    // AddStudent(t2,CreateStu(3,"xiaogang",22));
    // PrintTeach(t2);
    // ClearTeach(t1);
    // ClearTeach(t2);



    INITDARRAY(TeachArray);
    TeachArray.MyInsertArrayTail(&TeachArray,CreateTeach("张三",40));
    TeachArray.MyInsertArrayTail(&TeachArray,CreateTeach("李四",50));
    TeachArray.MyInsertArrayTail(&TeachArray,CreateTeach("张四",60));

    DArray arr = TeachArray.MyFindElement(&TeachArray,(void *)"张",CheckNameContain);
    TeachArray.MyDisplayDynamicArray(&arr,PrintTeach);
    TeachArray.MyClearDynamicArray(&TeachArray,ClearTeach);

    return 0;
}   